package com.lzy.business.controller.approval;
import com.github.pagehelper.PageInfo;
import com.lzy.approval.apis.EventServiceApi;
import com.lzy.approval.dtos.EventDto;
import com.lzy.business.controller.BaseController;
import com.lzy.business.facade.approval.EventFacade;
import com.lzy.business.request.EventRequest;
import com.lzy.business.response.EventResponse;
import com.lzy.common.constants.BusinessType;
import com.lzy.framework.component.PageBean;
import com.lzy.framework.core.ensure.Ensure;
import com.lzy.framework.core.orm.db.Pager;
import com.lzy.framework.core.output.Result;
import com.lzy.framework.core.utils.DozerUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * Description: 审批事件流程定义.
 * User:  Created by WIN7 on 2017/12/15.
 * Time: 9:21
 */
@Controller
@RequestMapping("/admin/event")
public class EventController extends BaseController {

    @Autowired
    private EventServiceApi eventServiceApi;

    @Autowired
    private EventFacade eventFacade;

//    @RequiresPermissions("sys:event:view")
    @RequestMapping(value = "", method = RequestMethod.GET)
    public String eventProcessList() {
        return "approval/event_list";
    }

//    @RequiresPermissions("sys:process:view")
//    @RequestMapping(method = RequestMethod.GET)
//    public String list(Model model) {
//        PageInfo<EventDto> pageInfo = eventServiceApi.queryEventList(new Pager(1, Integer.MAX_VALUE, 0L), new EventDto());
//        if(null != pageInfo && null != pageInfo.getList() && pageInfo.getList().size() > 0) {
//            model.addAttribute("eventList", pageInfo.getList());
//        }
//        model.addAttribute("userId", Long.toString(UserUtil.getCurrentUser().getId()));
//        return "process/process_list";
//    }

//    @RequiresPermissions("sys:event:add")
    @RequestMapping(value = "/create", method = RequestMethod.GET)
    public String addMember(Model model) {
        model.addAttribute("action", "add");
        return "approval/event_add";
    }

    /**
     * 查询列表数据   返回json在页面上调用
     */
//    @RequiresPermissions("sys:event:view")
    @RequestMapping(value = "/json", method = RequestMethod.GET)
    @ResponseBody
    public PageBean queryEventList(EventRequest request) {
        Ensure.that(request).isNotNull("00000004");
        EventDto dto = DozerUtils.map(request, EventDto.class);
        Pager pager = new Pager();
        pager.setPageNum(request.getPageNum());
        pager.setPageSize(request.getPageSize());
        PageInfo<EventDto> pageInfo = eventServiceApi.queryEventList(pager, dto);
        Ensure.that(pageInfo).isNotNull("01001410");
        List<EventDto> list = pageInfo.getList();
        Ensure.that(list).isNotNull("01001410");
        if (pageInfo != null && pageInfo.getList() != null) {
            return new PageBean<EventResponse>(pageInfo.getTotal(), DozerUtils.maps(list, EventResponse.class));
        } else {
            return new PageBean();
        }
    }

    /**
     * 新增审核事件
     * @param request
     * @return
     */
//    @RequiresPermissions("sys:event:add")
    @RequestMapping(value = "add", method = RequestMethod.POST)
    @ResponseBody
    public Result memberCreate(EventRequest request) {
        Ensure.that(request).isNotNull("00000004");
        request.setCreateUser(getLoginUserID());
        List<EventDto> eventList = eventServiceApi.queryEventListByResourceId(request.getResourceId());
        if (eventList != null) {
            for (EventDto dto : eventList) {
                dto.setStatus(BusinessType.StatusEnum.DISABLED.getCode());
                eventServiceApi.updateStatus(dto);
            }
        }
        Boolean ok = eventFacade.eventCreate(DozerUtils.map(request, EventDto.class));
        Ensure.that(ok).isTrue("01001411");
        return new Result();
    }

    /**
     * 启用
     *
     * @param request
     * @return
     */
    @RequestMapping(value = "startUser", method = RequestMethod.POST)
    @ResponseBody
    public Result startUser(EventRequest request) {
        Ensure.that(request).isNotNull("00000004");
        request.setCreateUser(getLoginUserID());
        request.setStatus(BusinessType.StatusEnum.ENABLED.getCode());
        Boolean ok = eventFacade.updateStatus(DozerUtils.map(request, EventDto.class));
        Ensure.that(ok).isTrue("01001403");
        return new Result();
    }

    /**
     * 禁用
     *
     * @param request
     * @return
     */
    @RequestMapping(value = "cancelUser", method = RequestMethod.POST)
    @ResponseBody
    public Result cancelUser(EventRequest request) {
        Ensure.that(request).isNotNull("00000004");
        request.setCreateUser(getLoginUserID());
        request.setStatus(BusinessType.StatusEnum.DISABLED.getCode());
        Boolean ok = eventServiceApi.updateStatus(DozerUtils.map(request, EventDto.class));
        Ensure.that(ok).isTrue("01001403");
        return new Result();
    }

    /**
     * 获取数据列表转向编辑页面
     *
     * @param id
     * @param model
     * @return
     */
//    @RequiresPermissions("sys:event:update")
    @RequestMapping(value = "/toEdit/{id}")
    public String toEdit(@PathVariable("id") Long id, Model model) {
        Ensure.that(id).isNotNull("00000004");
        EventDto eventDto = eventFacade.queryEventProcessById(id);
        Ensure.that(eventDto).isNotNull("01001415");
        EventResponse response = DozerUtils.map(eventDto, EventResponse.class);
        model.addAttribute("event", response);
        return "approval/event_edit";
    }

    /**
     * 更新数据
     */
//    @RequiresPermissions("sys:event:update")
    @RequestMapping(value = "update", method = RequestMethod.POST)
    @ResponseBody
    public Result memberUpdate(EventRequest request) {
        Ensure.that(request).isNotNull("00000004");
        request.setUpdateUser(getLoginUserID());
        EventDto eventDto = DozerUtils.map(request, EventDto.class);
        eventDto.setId(request.getId());
        eventDto.setStatus(request.getStatus());
        Boolean ok = eventFacade.eventUpdate(eventDto);
        Ensure.that(ok).isTrue("01001407");
        return new Result();
    }
}
